package com.hzhr.iot.device.service;

import com.hzhr.iot.core.annotation.ApiImplicitInternalParams;
import com.hzhr.iot.core.annotation.CygQuery;
import com.hzhr.iot.core.annotation.CygQuerys;
import com.hzhr.iot.core.annotation.CygService;
import com.hzhr.iot.core.constant.AccessModeEnum;
import com.hzhr.iot.core.constant.CommonOptions;
import com.hzhr.iot.core.exception.ServiceException;
import com.hzhr.iot.core.util.HzhrTypeUtil;
import com.hzhr.iot.device.constant.DeviceOptions;
import com.hzhr.iot.device.constant.Device_ErrorsEnum;
import com.hzhr.iot.device.constant.UserOptions;
import com.hzhr.iot.device.convertor.custom.IotDeviceCustomConvertor;
import com.hzhr.iot.device.dao.IotDeviceDao;
import com.hzhr.iot.device.dao.helper.IotDeviceDaoHelper;
import com.hzhr.iot.device.entity.IotDeviceEntity;
import com.hzhr.iot.device.entity.ext.IotDeviceEntityExt;
import com.hzhr.iot.device.model.iotDevice.IotDeviceGetModel;
import com.hzhr.iot.device.model.iotDevice.IotDeviceModel;
import com.hzhr.iot.device.model.iotDevice.IotDevicePutModel;
import com.hzhr.iot.device.queryModel.custom.IotDeviceCustomQueryModel;
import com.hzhr.iot.device.service.base.BaseService;
import com.hzhr.iot.device.service.helper.CollectDeviceServiceHelper;
import com.hzhr.iot.device.service.helper.User_CompanyServiceHelper;
import com.hzhr.iot.device.service.helper.User_DeptServiceHelper;
import com.hzhr.iot.login.sdk.model.userCard.UserCardModel;
import com.hzhr.iot.user.sdk.model.company.CompanyModel;
import com.hzhr.iot.user.sdk.service.User_CompanyService;
import com.hzhr.iot.user.sdk.service.User_DeptService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.*;

@Service
@CygService(entity = IotDeviceEntity.class)
@CygQuerys({
        @CygQuery(name = "_equal_id", query = "equal_id", type = Integer.class),
        @CygQuery(name = "_equal_code", query = "equal_code", type = String.class),
        @CygQuery(name = "_equal_categoryCode", query = "equal_categoryCode", type = String.class),
        @CygQuery(name = "_equal_iotPlatformImei", query = "equal_iotPlatformImei", type = String.class),
        @CygQuery(name = "_equal_iotPlatformDeviceId", query = "equal_iotPlatformDeviceId", type = String.class),
        @CygQuery(name = "_like_code", query = "like_code", type = String.class),
        @CygQuery(name = "_like_resourceCode", query = "like_resourceCode", type = String.class),
        @CygQuery(name = "_isnull_gasCompanyId", query = "isnull_gasCompanyId", type = Boolean.class),
        @CygQuery(name = "_greaterthanorequal_createTime", query = "greaterthanorequal_createTime", type = Date.class),
        @CygQuery(name = "_lessthan_createTime", query = "lessthan_createTime", type = Date.class),

})
public class IotDeviceService extends BaseService {
    @Autowired
    IotDeviceCustomConvertor iotDeviceCustomConvertor;
    @Autowired
    IotDeviceDao iotDeviceDao;
    @Autowired
    IotDeviceService iotDeviceService;
    @Autowired
    User_CompanyService user_companyService;
    @Autowired
    User_DeptService user_deptService;
    @Autowired
    User_DeptServiceHelper user_deptServiceHelper;
    @Autowired
    DeviceOptions deviceOptions;
    @Autowired
    CollectDeviceServiceHelper collectDeviceServiceHelper;
    @Autowired
    User_CompanyServiceHelper user_companyServiceHelper;
    @Autowired
    UserOptions userOptions;
    @Autowired
    IotDeviceDaoHelper iotDeviceDaoHelper;

    //region javadoc注释 : get
    /**
     * 查询用户管辖范围下的物联网设备 <br/><br/>
     * 步骤 : <br/>
     * 1. 查询物联网设备： <br/>
     *  如果登录用户是燃气公司用户，可查询用户管辖范围下的物联网设备； <br/>
     *  如果登录用户是物联网公司用户，可查询用户所在公司下的物联网设备； <br/>
     *  如果是全局用户(用户编号为0)，可查询所有物联网设备； <br/>
     * 2. 返回信息 <br/>
     * @param header <br/>
     * _token : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 令牌;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-code : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户代码;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-name : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户名;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-company-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 公司编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-dept-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 部门编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户类型(关联: common_service.options.code, typeCode = 'USER_TYPE');<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易流水号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易类型;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-access-mode : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 访问模式 0:一般 1:全局;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : 0;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/><br/>
     * @param query <br/>
     * _show : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 展示形式 one:展示一个,list:展示列表,null(空值):不展示;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _show_total : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 是否展示总数;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : boolean;<br/>
     * _show_info : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 是否展示详情;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : boolean;<br/>
     * _show_company : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 是否展示所属公司信息;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : boolean;<br/>
     * _show_gasCompany : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 是否展示所属燃气公司信息;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : boolean;<br/>
     * _show_outId : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 是否展示外部系统唯一键;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : boolean;<br/>
     * _page : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 页数;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : 1;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _count : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 每页数量;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : 1;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _equal_id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件 : where iotDevice.id = #{_equal_id};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _equal_code : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件 : where iotDevice.code = #{_equal_code};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _equal_categoryCode : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件 : where iotDevice.categoryCode = #{_equal_categoryCode};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _equal_iotPlatformImei : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件 : where iotDevice.iotPlatformImei = #{_equal_iotPlatformImei};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _equal_iotPlatformDeviceId : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件 : where iotDevice.iotPlatformDeviceId = #{_equal_iotPlatformDeviceId};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _like_resourceCode : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件 : where iotDevice.resourceCode like #{_like_resourceCode};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _like_code : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件 : where iotDevice.code like #{_like_code};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _isnull_gasCompanyId : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件 : where iotDevice.gasCompanyId is (not) null;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : boolean;<br/>
     * _greaterthanorequal_createTime : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件 : where iotDevice.createTime >= #{_greaterthanorequal_createTime};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : long;<br/>
     * _lessthan_createTime : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件 : where iotDevice.createTime < #{_lessthan_createTime};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : long;<br/><br/>
     * @throws ServiceException <br/>
     * @see IotDeviceGetModel result <br/>
     */ 
    //endregion
    //region 说明 : get
    //查询用户管辖范围下的物联网设备
    //步骤 :
    //1. 查询物联网设备：
    // 如果登录用户是燃气公司用户，可查询用户管辖范围下的物联网设备；
    // 如果登录用户是物联网公司用户，可查询用户所在公司下的物联网设备；
    // 如果是全局用户(用户编号为0)，可查询所有物联网设备；
    //2. 返回信息
    //endregion
    //region 参数 : get
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "令牌",required = true, dataType = "string", paramType = "header"),

            @ApiImplicitParam(name = "show", value = "展示形式 one:展示一个,list:展示列表,null(空值):不展示", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "show_total", value = "是否展示总数", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "show_info", value = "是否展示详情", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "show_company", value = "是否展示所属公司信息", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "show_gasCompany", value = "是否展示所属燃气公司信息", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "show_outId", value = "是否展示外部系统唯一键", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "页数", defaultValue = "1",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "count", value = "每页数量", defaultValue = "1",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "equal_id", value = "筛选条件 : where iotDevice.id = #{equal_id}",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "equal_code", value = "筛选条件 : where iotDevice.code = #{equal_code}",required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "equal_categoryCode", value = "筛选条件 : where iotDevice.categoryCode = #{equal_categoryCode}",required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "equal_iotPlatformImei", value = "筛选条件 : where iotDevice.iotPlatformImei = #{equal_iotPlatformImei}",required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "equal_iotPlatformDeviceId", value = "筛选条件 : where iotDevice.iotPlatformDeviceId = #{equal_iotPlatformDeviceId}",required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "like_code", value = "筛选条件 : where iotDevice.code like #{like_code}",required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "isnull_gasCompanyId", value = "筛选条件 : where iotDevice.gasCompanyId is (not) null",required = false, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "greaterthanorequal_createTime", value = "筛选条件 : where iotDevice.createTime >= #{greaterthanorequal_createTime}",required = false, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "lessthan_createTime", value = "筛选条件 : where iotDevice.createTime < #{lessthan_createTime}",required = false, dataType = "long", paramType = "query"),

    })
    @ApiImplicitInternalParams({
            @ApiImplicitParam(name = "_token", value = "令牌",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-id", value = "用户编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-code", value = "用户代码",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-name", value = "用户名",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-company-id", value = "公司编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-dept-id", value = "部门编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-type", value = "用户类型(关联: common_service.options.code, typeCode = 'USER_TYPE')",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-transaction-id", value = "交易流水号",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-transaction-type", value = "交易类型",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-access-mode", value = "访问模式 0:一般 1:全局",defaultValue = "0", required = false, dataType = "int", paramType = "header"),

            @ApiImplicitParam(name = "_show", value = "展示形式 one:展示一个,list:展示列表,null(空值):不展示", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "_show_total", value = "是否展示总数", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "_show_info", value = "是否展示详情", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "_show_company", value = "是否展示所属公司信息", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "_show_gasCompany", value = "是否展示所属燃气公司信息", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "_show_outId", value = "是否展示外部系统唯一键", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "_page", value = "页数", defaultValue = "1",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "_count", value = "每页数量", defaultValue = "1",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "_equal_id", value = "筛选条件 : where iotDevice.id = #{_equal_id}",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "_equal_code", value = "筛选条件 : where iotDevice.code = #{_equal_code}",required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "_equal_categoryCode", value = "筛选条件 : where iotDevice.categoryCode = #{_equal_categoryCode}",required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "_equal_iotPlatformImei", value = "筛选条件 : where iotDevice.iotPlatformImei = #{_equal_iotPlatformImei}",required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "_equal_iotPlatformDeviceId", value = "筛选条件 : where iotDevice.iotPlatformDeviceId = #{_equal_iotPlatformDeviceId}",required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "_like_resourceCode", value = "筛选条件 : where iotDevice.resourceCode like #{_like_resourceCode}",required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "_like_code", value = "筛选条件 : where iotDevice.code like #{_like_code}",required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "_isnull_gasCompanyId", value = "筛选条件 : where iotDevice.gasCompanyId is (not) null",required = false, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "_greaterthanorequal_createTime", value = "筛选条件 : where iotDevice.createTime >= #{_greaterthanorequal_createTime}",required = false, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "_lessthan_createTime", value = "筛选条件 : where iotDevice.createTime < #{_lessthan_createTime}",required = false, dataType = "long", paramType = "query"),
    })
    //endregion
    //region 请求 : get
    @RequestMapping(value = "/iot-devices", method = RequestMethod.GET)
    //endregion
    public IotDeviceGetModel get(Map<String, String> header, Map<String, Object> query) throws ServiceException {
        /*查询物联网设备：
            如果登录用户是燃气公司用户，查询用户管辖范围下的物联网设备；
            如果登录用户是物联网公司用户，查询用户所在公司下的物联网设备；*/
        IotDeviceCustomQueryModel queryModel = iotDeviceCustomConvertor.iotDeviceService_query2queryModel(query);

        UserCardModel userCard = getUserCard(header);
        if (userCard.getUserId() != 0) {
            CompanyModel company = user_companyServiceHelper.getById(initHeader(header, AccessModeEnum.GLOBAL), userCard.getCompanyId());
            String companyType = company.getType();
            if ("1".equals(companyType)) {
                queryModel.setLike_resourceCode(getUserCode(header) + "%");
            } else if ("2".equals(companyType)) {
                queryModel.setEqual_companyId(userCard.getCompanyId());
            }
        }

        queryModel.setEqual_isValid(CommonOptions.YES_NO.YES);
        //查询实体
        List<IotDeviceEntityExt> extList = null;
        String show = HzhrTypeUtil.object2string(query.get("_show"));
        if (!StringUtils.isEmpty(show)) {
            extList = iotDeviceDao.selectExtList(queryModel);
        }
        //查询数量
        Integer total = null;
        Boolean showTotal = HzhrTypeUtil.object2boolean(query.get("_show_total"));
        if (showTotal != null && showTotal) {
            total = iotDeviceDao.selectCount(queryModel);
        }

        /* 返回信息 */
        IotDeviceGetModel result = iotDeviceCustomConvertor.extList2iotDeviceGetModel(extList, total, null, query);
        //查询公司信息
        Boolean showCompany = HzhrTypeUtil.object2boolean(query.get("_show_company"));
        Boolean showGasCompany = HzhrTypeUtil.object2boolean(query.get("_show_gasCompany"));
        List<CompanyModel> companyModels = null;
        List<CompanyModel> gasCompanyModels = null;
        if (extList != null) {
            if (showCompany != null && showCompany) {
                List<Integer> companyIdList = new ArrayList<>();
                for (IotDeviceEntityExt ext :
                        extList) {
                    if (ext.getCompanyId() != null) {
                        companyIdList.add(ext.getCompanyId());
                    }
                }

                Map<String, Object> query1 = new HashMap<>();
                query1.put("_show", "list");
                query1.put("_show_info", true);
                query1.put("_in_id", companyIdList);
                companyModels = user_companyService.get(initHeader(header, AccessModeEnum.GLOBAL), query1).getModelList();
            }
            if (showGasCompany != null && showGasCompany) {
                List<Integer> companyIdList = new ArrayList<>();
                for (IotDeviceEntityExt ext :
                        extList) {
                    if (ext.getGasCompanyId() != null) {
                        companyIdList.add(ext.getGasCompanyId());
                    }
                }

                Map<String, Object> query1 = new HashMap<>();
                query1.put("_show", "list");
                query1.put("_show_info", true);
                query1.put("_in_id", companyIdList);
                gasCompanyModels = user_companyService.get(initHeader(header, AccessModeEnum.GLOBAL), query1).getModelList();
            }
        }
        //在响应中添加额外信息
        List<IotDeviceModel> iotDeviceModels = result.getModelList();
        if (iotDeviceModels != null) {
            for (int i = 0; i < iotDeviceModels.size(); i++) {
                IotDeviceModel iotDeviceModel = iotDeviceModels.get(i);
                if (companyModels != null) {
                    for (CompanyModel companyModel :
                            companyModels) {
                        if (companyModel.getId().equals(iotDeviceModel.getCompanyId())) {
                            iotDeviceModel.setCompanyName(companyModel.getName());
                            iotDeviceModel.setCompanyShortName(companyModel.getShortName());
                        }
                    }
                }
                if (gasCompanyModels != null) {
                    for (CompanyModel gasCompanyModel :
                            gasCompanyModels) {
                        if (gasCompanyModel.getId().equals(iotDeviceModel.getGasCompanyId())) {
                            iotDeviceModel.setGasCompanyName(gasCompanyModel.getName());
                            iotDeviceModel.setGasCompanyShortName(gasCompanyModel.getShortName());
                        }
                    }
                }
            }
        }
        IotDeviceModel iotDeviceModel = result.getModel();
        if (iotDeviceModel != null) {
            if (companyModels != null && companyModels.size() > 0) {
                CompanyModel companyModel = companyModels.get(0);
                iotDeviceModel.setCompanyName(companyModel.getName());
                iotDeviceModel.setCompanyShortName(companyModel.getShortName());
            }
            if (gasCompanyModels != null && gasCompanyModels.size() > 0) {
                CompanyModel gasCompanyModel = gasCompanyModels.get(0);
                iotDeviceModel.setGasCompanyName(gasCompanyModel.getName());
                iotDeviceModel.setGasCompanyShortName(gasCompanyModel.getShortName());
            }
        }
        return result;
    }

    //region javadoc注释 : putById
    /**
     * 更新物联网设备 <br/><br/>
     * 简介 : 更新物联网设备 <br/>
     * 步骤 : <br/>
     * 1. 查询物联网设备 ; (√) <br/>
     *  根据路径参数id查询物联网设备(IOT_DEVICE)iotDevice_1, <br/>
     *  路径参数查询筛选规则： <br/>
     *           (1) iotDevice_1.id = id； <br/>
     *  如果iotDevice_1不存在，抛出异常201； <br/>
     * 2. 验证新的物联网设备代码 : (√) <br/>
     * 如果body.code != iotDevice_1.code，且存在某个物联网设备(IOT_DEVICE)对象iotDevice满足iotDevice.code = body.code，则抛出异常202； <br/>
     * 3. 更新物联网设备的注册信息 : (√) <br/>
     * 根据body更新iotDevice_1的相关信息； <br/>
     * 4. 返回信息 : (√) <br/>
     * 根据条件参数查询iotDevice_1更新后的信息，然后生成结果对象并返回； <br/>
     * @param header <br/>
     * _token : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 令牌;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-code : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户代码;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-name : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户名;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-company-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 公司编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-dept-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 部门编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户类型(关联: common_service.options.code, typeCode = 'USER_TYPE');<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易流水号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易类型;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-access-mode : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 访问模式 0:一般 1:全局;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : 0;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/><br/>
     * @param query <br/>
     * _show : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 展示形式 one:展示一个,list:展示列表,null(空值):不展示;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/><br/>
     * @param id 物联网设备编号 <br/><br/>
     * @param body <br/><br/>
     * @throws ServiceException <br/>
     * IOTDEVICESERVICE_PUT_BY_ID_201 : <br/>&nbsp&nbsp&nbsp&nbsp异常类型 : 业务流程异常<br/>&nbsp&nbsp&nbsp&nbsp异常业务 : 查询物联网设备<br/>&nbsp&nbsp&nbsp&nbsp异常描述 : 物联网设备不存在<br/>
     * IOTDEVICESERVICE_PUT_BY_ID_202 : <br/>&nbsp&nbsp&nbsp&nbsp异常类型 : 业务流程异常<br/>&nbsp&nbsp&nbsp&nbsp异常业务 : 验证新的物联网设备代码<br/>&nbsp&nbsp&nbsp&nbsp异常描述 : 物联网设备代码已存在<br/>
     * @see IotDevicePutModel body <br/>
     * @see IotDeviceGetModel result <br/>
     */ 
    //endregion
    //region 说明 : putById
    //更新物联网设备
    //简介 : 更新物联网设备
    //步骤 :
    //1. 查询物联网设备 ; (√)
    // 根据路径参数id查询物联网设备(IOT_DEVICE)iotDevice_1,
    // 路径参数查询筛选规则：
    //          (1) iotDevice_1.id = id；
    // 如果iotDevice_1不存在，抛出异常201；
    //2. 验证新的物联网设备代码 : (√)
    //如果body.code != iotDevice_1.code，且存在某个物联网设备(IOT_DEVICE)对象iotDevice满足iotDevice.code = body.code，则抛出异常202；
    //3. 更新物联网设备的注册信息 : (√)
    //根据body更新iotDevice_1的相关信息；
    //4. 返回信息 : (√)
    //根据条件参数查询iotDevice_1更新后的信息，然后生成结果对象并返回；
    //endregion
    //region 参数 : putById
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "令牌",required = true, dataType = "string", paramType = "header"),

            @ApiImplicitParam(name = "id", value = "物联网设备编号",required = true, dataType = "int", paramType = "path"),

            @ApiImplicitParam(name = "show", value = "展示形式 one:展示一个,list:展示列表,null(空值):不展示", required = false, dataType = "string", paramType = "query"),

    })
    @ApiImplicitInternalParams({
            @ApiImplicitParam(name = "_token", value = "令牌",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-id", value = "用户编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-code", value = "用户代码",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-name", value = "用户名",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-company-id", value = "公司编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-dept-id", value = "部门编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-type", value = "用户类型(关联: common_service.options.code, typeCode = 'USER_TYPE')",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-transaction-id", value = "交易流水号",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-transaction-type", value = "交易类型",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-access-mode", value = "访问模式 0:一般 1:全局",defaultValue = "0", required = false, dataType = "int", paramType = "header"),

            @ApiImplicitParam(name = "_show", value = "展示形式 one:展示一个,list:展示列表,null(空值):不展示", required = false, dataType = "string", paramType = "query"),

    })
    //endregion
    //region 请求 : putById
    @RequestMapping(value = "/iot-devices/{id}", method = RequestMethod.PUT)
    //endregion
    @Transactional(rollbackFor = Exception.class)
    public IotDeviceGetModel putById(Map<String, String> header, Integer id,
                                     Map<String, Object> query, IotDevicePutModel body) throws ServiceException {
        /* 查询物联网设备 */
        IotDeviceCustomQueryModel queryModel = new IotDeviceCustomQueryModel();
        queryModel.setEqual_id(id);
        queryModel.setEqual_isValid(CommonOptions.YES_NO.YES);
        queryModel.setShow(true);
        IotDeviceEntity iotDevice_1 = iotDeviceDao.selectEntity(queryModel);
        if (iotDevice_1 == null) {
            throw new ServiceException(Device_ErrorsEnum.IOTDEVICESERVICE_PUT_BY_ID_201);
        }

        /* 验证新的物联网设备代码 */
        String code_1 = iotDevice_1.getCode();
        String code = body.getCode();
        if (!code_1.equals(code)) {
            queryModel = new IotDeviceCustomQueryModel();
            queryModel.setEqual_code(code);
            queryModel.setEqual_isValid(CommonOptions.YES_NO.YES);
            int count = iotDeviceDao.selectCount(queryModel);
            if (count > 0) {
                throw new ServiceException(Device_ErrorsEnum.IOTDEVICESERVICE_PUT_BY_ID_202);
            }
        }

        /* 更新物联网设备的注册信息 */
        iotDevice_1 = iotDeviceCustomConvertor.iotDevicePutModel2entity(body, iotDevice_1);
        iotDevice_1.setUpdateTime(new Date());
        iotDevice_1.setUpdateUserId(getUserId(header));
        int changeCount = iotDeviceDao.update(iotDevice_1);

        /* 返回信息 */
        query.put("_equal_id", iotDevice_1.getId());
        IotDeviceGetModel result = iotDeviceService.get(initHeader(header, AccessModeEnum.GLOBAL), query);
        result.setChangeCount(changeCount);
        return result;
    }

}

